#include "head.h"

int lexicalanalysis(char* buf, ssize_t buflen, int netfd){
    int strcount = 0;//查找需要分割的字符串个数
    int count = 0;
    char command[128] = {0};
    char path[1024] = {0};
    char filename[1024] = {0};

    if(isspace(buf[0]) != 0){//命令前有空格或空白字符
        for(int i =0; i < buflen - 1; ++i){
            if((isspace(buf[i]) != 0) && (isspace(buf[i+1]) == 0)){
                if((isalnum(buf[i+1]) != 0) || (buf[i+1] == '/') || (buf[i+1] == '.')){
                    strcount++;

                }else{
                    printf("plase input again!\n");
                    return -1;
                }
            }
        }
        if(strcount > 3){
            printf("plase inpute again!\n");
            return -1;
        }
        strcpy(command, strtok(buf, " "));
        switch(strcount){
        case 1:if(strncmp("ls", command, 2) == 0){
                   ls_dir(netfd);
               }else if(strncmp("pwd", command, 3) == 0){
                   pwd(netfd);
               }break;

        case 2:if(strncmp("cd", command, 2) == 0){
                   strcpy(path, strtok(NULL, " "));
                   int ret = cd_dir(netfd, path);
                   if(ret == 0){
                       printf("%s", path);
                       fflush(STDIN_FILENO);
                   }
               }break;

        case 3:if(strncmp("puts", command, 4) == 0){
                   strcpy(path, strtok(NULL, " "));
                   strcpy(filename, strtok(NULL, " "));
                   puts_file(netfd, filename, path);
               }else if(strncmp("gets", command, 4) == 0){
                   strcpy(path, strtok(NULL, " "));
                   strcpy(filename, strtok(NULL, " "));
                   gets_file(netfd, filename, path);
               }else if(strncmp("remove", command, 6) == 0){
                   strcpy(path, strtok(NULL, " "));
                   strcpy(filename, strtok(NULL, " "));
                   remove_file(netfd, filename, path);
               }else if(strncmp("mkdir", command, 5) == 0){
                   strcpy(path, strtok(NULL, " "));
                   strcpy(filename, strtok(NULL, " "));
                   mkdir_dir(netfd, filename, path);
               }else if(strncmp("rmdir", command, 5) == 0){
                   strcpy(path, strtok(NULL, " "));
                   strcpy(filename, strtok(NULL, " "));
                   rmdir_dir(netfd, filename, path);
               }break;
        }



    }else if(isspace(buf[0]) == 0){
        //命令前没有空白字符
        char* p;
        char temp[1024] = {0};
        strncpy(temp, buf, buflen);
        p = strtok(temp, " ");
        while(p){
            p = strtok(NULL, " ");
            ++count;
        }

        if(count > 3){
            printf("plase inpute again!\n");
            return -1;
        }
        strcpy(command, strtok(buf, " "));
        switch(count){
        case 1:if(strncmp("ls", command, 2) == 0){
                   ls_dir(netfd);
               }else if(strncmp("pwd", command, 3) == 0){
                   pwd(netfd);
               }break;

        case 2:if(strncmp("cd", command, 2) == 0){
                   strcpy(path, strtok(NULL, " "));
                   int ret = cd_dir(netfd, path);
                   //if(ret == 0){
                     //  printf("%s", path);
                     //  fflush(STDIN_FILENO);
                  // }
               }break;

        case 3:if(strncmp("puts", command, 4) == 0){
                   strcpy(path, strtok(NULL, " "));
                   strcpy(filename, strtok(NULL, " "));
                   puts_file(netfd, filename, path);
               }else if(strncmp("gets", command, 4) == 0){
                   strcpy(path, strtok(NULL, " "));
                   strcpy(filename, strtok(NULL, " "));
                   gets_file(netfd, filename, path);
               }else if(strncmp("remove", command, 6) == 0){
                   strcpy(path, strtok(NULL, " "));
                   strcpy(filename, strtok(NULL, " "));
                   remove_file(netfd, filename, path);
               }else if(strncmp("mkdir", command, 5) == 0){
                   strcpy(path, strtok(NULL, " "));
                   strcpy(filename, strtok(NULL, " "));
                   mkdir_dir(netfd, filename, path);
               }else if(strncmp("rmdir", command, 5) == 0){
                   strcpy(path, strtok(NULL, " "));
                   strcpy(filename, strtok(NULL, " "));
                   rmdir_dir(netfd, filename, path);
               }break;
        }
    }
    return 0;
}
